<%@ page import="com.automatedlogic.domino.sso.login.DominoLoginFailure,lotus.domino.NotesError"%>
<%@ page import="org.apache.commons.lang.StringUtils" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
%>

<%
    /*=============================================================================
    This file is part of DominoTomcatSSOIntegration.
    http://www.automatedlogic.com/domblog.nsf/dx/DominoTomcatSSOIntegration

    If the user's web browser has Cookies disabled, they will see a message
    explaining that "cookies are required".  (You can customize the help text below.)

    When a login fails, the user will see additional information on their login form.
    (You can customize the help text below.)

    The login form will send a Post to the DominoLoginServlet.  This Servlet will
    authenticate the user, and store an LtpaToken cookie in their web browser.
    The user will then be redirected to the requested web page.

    OPTIONAL.  You may wish to use a simple login form, where the Post is sent
    to the Domino web server instead of a Servlet.  This may be preferred if you
    require an SSL connection for your login form, you already have SSL working
    on your Domino web server, and do not wish to add SSL to your Tomcat web server.
    If this is the case, you can delete this login.jsp file, and rename the
    file login-simple.jsp to login.jsp.  Edit that file and enter a new
    value for dominoLoginSiteURL.
    =============================================================================*/

    ServletContext context = this.getServletConfig().getServletContext();

    //Custom text for the login form.  Defined in web.xml.
    String bannerMessage = context.getInitParameter("BannerMessage") == null ? "Welcome" : context.getInitParameter("BannerMessage");
    String welcomeMessage = context.getInitParameter("WelcomeMessage") == null ? "Use your Domino User name and Password." : context.getInitParameter("WelcomeMessage");
    String imageURL = context.getInitParameter("ImageURL") == null || context.getInitParameter("ImageURL").equals("") ? "" : "<img src=\"" + context.getInitParameter("ImageURL") + "\" border=\"0\"/>";

    //Page requested by the user.
    //For example, when the user opens this form using http://tomcat.acme.com/login/?page=http://tomcat.acme.com/myapp?aaa=1&bbb=2&ccc=3
    //then the value for redirectTo will be http://tomcat.acme.com/myapp?aaa=1&bbb=2&ccc=3
    String queryString = request.getQueryString();
    String redirectTo;
    if( queryString == null ) {
        redirectTo = "/";
    } else {
        redirectTo = queryString.substring(queryString.indexOf("page=") + 5);
    }

    //Message to appear after a failed login.  The user's session will contain an object named DominoLoginFailure.
    //Use these properties to discover the login-error: getDominoErrorID(), and getDominoErrorText().
    //You can display a custom message based on the error.
    DominoLoginFailure dominoLoginFailure = (DominoLoginFailure) session.getAttribute("DominoLoginFailure");
    String errorMessage = "";
    String errorHelp = "";
    String cookieHelp = "";
    if (dominoLoginFailure != null) {

        switch (dominoLoginFailure.getDominoErrorID()) {

            //Invalid password.
            case NotesError.NOTES_ERR_INVALID_USERNAME_PASSWD:
                errorMessage = "Invalid user name or password. Please try again.";
                break;

            //Invalid username.
            case NotesError.NOTES_ERR_INVALID_USERNAME:
                errorMessage = "Invalid user name or password. Please try again.";
                break;

            //Access denied.
            case NotesError.NOTES_ERR_SERVER_ACCESS_DENIED:
                errorMessage = "Sorry.  You are not authorized to use this login server.";
                break;

            //Could not get IOR from Domino Server.  The Domino server may be down, or the DIIOP task is not running.
            case NotesError.NOTES_ERR_GETIOR_FAILED:
                errorMessage = "Sorry.  The login server is unavailable.  Please try again later.";
                break;

            //Other
            default:
                errorMessage = dominoLoginFailure.getDominoErrorText();
        }

        //Additional help text to appear after a failed login.
        if( dominoLoginFailure.getDominoErrorID() == NotesError.NOTES_ERR_INVALID_USERNAME_PASSWD | dominoLoginFailure.getDominoErrorID() == NotesError.NOTES_ERR_INVALID_USERNAME ) {
            errorHelp = "<hr/>" +
            "<center>" +
            "    <b>Invalid username or password was specified. <br/> Passwords are case-sensitive.</b>" +
            "</center>" +
            "<br/>" +
            "<br/>" +
            "Cookies are required for user authentication.  " +
            "If you do not accept the cookies set on login or your computer is not configured to accept cookies, your session will expire almost immediately." +
            "<br/>" +
            "<br/>" +
            "If you see the logout message immediately after logging in, do the following:" +
            "<ul>" +
            "    <li> Set up your browser to accept cookies or agree to accept cookies during login.</li>" +
            "    <li> Turn off any third-party programs or control panels that automatically reject cookies.</li>" +
            "    <li> Make sure your computer system time is accurate. Cookies are time-sensitive.</li>" +
            "</ul>";
        }

    }

    //This text will only appear if the user has Cookies disabled in their web browser.  The login form will not work when Cookies are disabled.
    cookieHelp = "<hr/>" +
    "<center>" +
    "<b>Cookies are required for user authentication.</b>" +
    "</center>" +
    "<br/>" +
    "Please do the following:" +
    "<br/>" +
    "<ul>" +
    "    <li> Set up your browser to accept cookies or agree to accept cookies during login.</li>" +
    "    <li> Turn off any third-party programs or control panels that automatically reject cookies.</li>" +
    "    <li> Make sure your computer system time is accurate. Cookies are time-sensitive.</li>" +
    "</ul>";
%>







<%

String css = basePath+session.getServletContext().getInitParameter("portal-name")+"/css/global.css";

String cookieName="captive-cookie-id";
String captiveName = "";
Cookie[] cookies = request.getCookies();
if (cookies != null) {
   for(int i=0; i<cookies.length; i++) {
    Cookie cookie = cookies[i];
    if (cookieName.compareToIgnoreCase(cookie.getName())==0)
    if(cookie.getValue()!=null && cookie.getValue().compareToIgnoreCase("null")!=0){
     css =  basePath+session.getServletContext().getInitParameter("portal-name")+"/css/"+(cookie.getValue().toLowerCase()+".css");
     captiveName=cookie.getValue().toLowerCase();
     captiveName = StringUtils.capitalize(captiveName);
     session.setAttribute("captive-name",captiveName);
     }
   }
  }

String logo = basePath+session.getServletContext().getInitParameter("portal-name")+"/img/"+captiveName.toLowerCase()+"/logo.gif";
%>

<html>
<head>
    <title>Sign In</title>
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="expires" content="-1"/>
    <link rel="stylesheet" type="text/css" href="<%=css%>"/>
    <script language="javascript" type="text/javascript" src="login.js"></script>
    <script language="javascript">
        function getCookieHelp() {
            return '<%= cookieHelp %>';
        }
    </script>
</head>

<body onLoad="document.loginForm.username.focus();">

<form name="loginForm" action="DominoLoginServlet" method="post">
<br/><br/>

<table align="center" valign="center" border="1" width="500" cellspacing="0" cellpadding="0">

    <tr>
        <td>

        <table border="0" cellpadding="14" cellspacing="0" width="100%">
            <tr bgcolor="#9FA193">
                <td align="center"><strong><%=captiveName %>  Portal</strong></td>
            </tr>
        </table>
		<div align="center">
		<br>
					<img src="<%=logo%>" alt="<%=captiveName%>" onError="this.src='<%=basePath+session.getServletContext().getInitParameter("portal-name")%>/img/core/blank.gif'"/>
				</div> 
        <br/>
        <br/>



        <table id="container" border="0" cellspacing="0" cellpadding="0" width="100%">
            <tr>
                <td>&nbsp;</td>
                <td align="center"><span class="bodyText">Use your <%=captiveName%>  Portal user name and password.</span></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align="center">
                    <br/>
                    <table width="100%" border="0" cellspacing="0" cellpadding="3">
                        <tr>
                            <td colspan="2">
                                <span class="errorMessageText"><center><%= errorMessage %></center></span>
                            </td>
                        </tr>
                        <tr>
                            <td align="right"><span class="loginText">User name&nbsp;&nbsp;</span></td>
                            <td width="60%"><input type="text" name="username" size="25" maxlength="256" value=""/></td>
                        </tr>
                        <tr>
                            <td align="right" width="40%"><span class="loginText">Password&nbsp;&nbsp;</span></td>
                            <td width="60%"><input type="password" name="password" size="25" maxlength="256" value=""/></td>
                        </tr>
                        <tr>
                            <td align="right"></td>
                            <td width="60%">
                                <input type="submit" class="button" name="submit" value="Sign In"/>
                                <br/>
                            </td>
                        </tr>
                    </table>
                    <br/>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align="center">
                    <!-- add a picture here -->
                    <%= imageURL %>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align="left">
                    <!-- add detailed help here -->
                    <br/>
                    <span class="errorHelpText">
                        <!-- Help text, when cookies are disabled -->
                        <script language="javascript">
                            if( cookiesAreDisabled() ) {
                                document.write( getCookieHelp() );
                            }
                        </script>
                        <!-- Help text, when login failed. -->
                        <%= errorHelp %>
                    </span>
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>


        </td>
    </tr>
</table>
	
<br/>
<input type="hidden" name="redirectto" value="<%= redirectTo %>"/>

</form>
</body>
</html>